package com.ny.service.impl;

import com.ny.mapper.MemberCardMapper;
import com.ny.mapper.MemberConsumptionMapper;
import com.ny.mapper.MemberLevelMapper;
import com.ny.mapper.MemberMapper;
import com.ny.domain.MemberCardDO;
import com.ny.domain.MemberConsumptionDO;
import com.ny.domain.MemberDO;
import com.ny.domain.MemberLevelDO;
import com.ny.dto.MemberCardDTO;
import com.ny.dto.MemberConsumptionDTO;
import com.ny.dto.MemberDTO;
import com.ny.dto.MemberLevelDTO;
import com.ny.query.MemberQuery;
import com.ny.service.MemberService;
import java.lang.Boolean;
import java.lang.Override;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import java.util.Date;

/**
 * 会员管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private MemberConsumptionMapper memberConsumptionMapper;

    @Autowired
    private MemberLevelMapper memberLevelMapper;

    @Override
    public List<MemberDO> queryMemberList(int pageNum, int pageSize) {
        List<MemberDO> memberDOList = memberMapper.queryMemberList(pageNum, pageSize);
        if (CollectionUtils.isEmpty(memberDOList)){
            MemberDO memberDO = new MemberDO();
            memberDO.setMemberId(1);
            memberDO.setName("小怡宝");
            memberDO.setContactInfo("16666666666");
            memberDO.setJoinDate(new Date());
            memberDO.setLevelId(1);
            memberDOList.add(memberDO);
        }
        return memberDOList;
    }

    @Override
    public Boolean addMember(MemberDTO memberDTO) {
        MemberDO memberDO = memberMapper.selectByNameAndContactInfo(memberDTO.getName(), memberDTO.getContactInfo());
        if (memberDO != null) {
            throw new BusinessException("会员已存在，请勿重复添加");
        }
        memberDO = new MemberDO();
        memberDO.setName(memberDTO.getName());
        memberDO.setContactInfo(memberDTO.getContactInfo());
        memberDO.setJoinDate(memberDTO.getJoinDate());
        memberDO.setLevelId(memberDTO.getLevelId());
        memberDO.setCreateBy("admin");
        memberDO.setCreateTime(new Date());
        int result = memberMapper.insert(memberDO);
        return result > 0;
    }

    @Override
    public Boolean deleteMember(MemberQuery memberQuery) {
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员不存在，请先添加会员信息");
        }
        memberMapper.deleteById(memberQuery.getMemberId());
        memberCardMapper.deleteByMemberId(memberQuery.getMemberId());
        return true;
    }

    @Override
    public Boolean updateMember(MemberDTO memberDTO) {
        MemberDO memberDO = memberMapper.selectById(memberDTO.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员不存在，请先添加会员信息");
        }
        memberDO.setName(memberDTO.getName());
        memberDO.setContactInfo(memberDTO.getContactInfo());
        memberDO.setLevelId(memberDTO.getLevelId());
        memberDO.setUpdateBy("admin");
        memberDO.setUpdateTime(new Date());
        int result = memberMapper.updateById(memberDO);
        return result > 0;
    }

    @Override
    public MemberDO getMemberInfo1(MemberQuery memberQuery) {
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员不存在，请先添加会员信息");
        }
        return memberDO;
    }

    @Override
    public Boolean addMemberCard(MemberCardDTO memberCardDTO) {
        MemberCardDO memberCardDO = memberCardMapper.selectByMemberIdAndType(memberCardDTO.getMemberId(), memberCardDTO.getCardType());
        if (memberCardDO != null) {
            throw new BusinessException("会员卡已存在，请勿重复添加");
        }
        memberCardDO = new MemberCardDO();
        memberCardDO.setMemberId(memberCardDTO.getMemberId());
        memberCardDO.setCardType(memberCardDTO.getCardType());
        memberCardDO.setBalance(memberCardDTO.getBalance());
        memberCardDO.setExpirationDate(memberCardDTO.getExpirationDate());
        memberCardDO.setCreateBy("admin");
        memberCardDO.setCreateTime(new Date());
        int result = memberCardMapper.insert(memberCardDO);
        return result > 0;
    }

    @Override
    public Boolean rechargeMemberCard(MemberCardDTO memberCardDTO) {
        MemberCardDO memberCardDO = memberCardMapper.selectById(memberCardDTO.getCardId());
        if (memberCardDO == null) {
            throw new BusinessException("会员卡不存在，请先添加会员卡信息");
        }
        memberCardDO.setBalance(memberCardDO.getBalance().add(memberCardDTO.getBalance()));
        memberCardDO.setUpdateBy("admin");
        memberCardDO.setUpdateTime(new Date());
        int result = memberCardMapper.updateById(memberCardDO);
        return result > 0;
    }

    @Override
    public Boolean renewMemberCard(MemberCardDTO memberCardDTO) {
        MemberCardDO memberCardDO = memberCardMapper.selectById(memberCardDTO.getCardId());
        if (memberCardDO == null) {
            throw new BusinessException("会员卡不存在，请先添加会员卡信息");
        }
        memberCardDO.setExpirationDate(memberCardDTO.getNewExpirationDate());
        memberCardDO.setUpdateBy("admin");
        memberCardDO.setUpdateTime(new Date());
        int result = memberCardMapper.updateById(memberCardDO);
        return result > 0;
    }

    @Override
    public List<MemberCardDO> getMemberCardInfo(MemberQuery memberQuery) {
        List<MemberCardDO> memberCardDOList = memberCardMapper.selectAllByMemberId(memberQuery.getMemberId());
        if (memberCardDOList == null || memberCardDOList.isEmpty()) {
            throw new BusinessException("会员卡不存在，请先添加会员卡信息");
        }
        return memberCardDOList;
    }

    @Override
    public Boolean addMemberConsumption(MemberConsumptionDTO memberConsumptionDTO) {
        MemberConsumptionDO memberConsumptionDO = new MemberConsumptionDO();
        memberConsumptionDO.setMemberId(memberConsumptionDTO.getMemberId());
        memberConsumptionDO.setAmount(memberConsumptionDTO.getAmount());
        memberConsumptionDO.setConsumptionDate(memberConsumptionDTO.getConsumptionDate());
        memberConsumptionDO.setDescription(memberConsumptionDTO.getDescription());
        memberConsumptionDO.setCreateBy("admin");
        memberConsumptionDO.setCreateTime(new Date());
        int result = memberConsumptionMapper.insert(memberConsumptionDO);
        return result > 0;
    }

    @Override
    public List<MemberConsumptionDO> getMemberConsumptionInfo(MemberQuery memberQuery) {
        List<MemberConsumptionDO> memberConsumptionDOList = memberConsumptionMapper.selectByMemberId(memberQuery.getMemberId());
        if (memberConsumptionDOList == null || memberConsumptionDOList.isEmpty()) {
            throw new BusinessException("会员消费记录不存在，请先添加会员消费信息");
        }
        return memberConsumptionDOList;
    }

    @Override
    public Boolean addMemberLevel(MemberLevelDTO memberLevelDTO) {
        MemberLevelDO memberLevelDO = memberLevelMapper.selectByName(memberLevelDTO.getLevelName());
        if (memberLevelDO != null) {
            throw new BusinessException("会员等级已存在，请勿重复添加");
        }
        memberLevelDO = new MemberLevelDO();
        memberLevelDO.setLevelName(memberLevelDTO.getLevelName());
        memberLevelDO.setDiscountRate(memberLevelDTO.getDiscountRate());
        memberLevelDO.setDescription(memberLevelDTO.getDescription());
        memberLevelDO.setCreateBy("admin");
        memberLevelDO.setCreateTime(new Date());
        int result = memberLevelMapper.insert(memberLevelDO);
        return result > 0;
    }

    @Override
    public Boolean updateMemberLevel(MemberLevelDTO memberLevelDTO) {
        MemberLevelDO memberLevelDO = memberLevelMapper.selectById(memberLevelDTO.getLevelId());
        if (memberLevelDO == null) {
            throw new BusinessException("会员等级不存在，请先添加会员等级信息");
        }
        memberLevelDO.setLevelName(memberLevelDTO.getLevelName());
        memberLevelDO.setDiscountRate(memberLevelDTO.getDiscountRate());
        memberLevelDO.setDescription(memberLevelDTO.getDescription());
        memberLevelDO.setUpdateBy("admin");
        memberLevelDO.setUpdateTime(new Date());
        int result = memberLevelMapper.updateById(memberLevelDO);
        return result > 0;
    }

    @Override
    public MemberLevelDO getMemberLevelInfo1(MemberQuery memberQuery) {
        MemberLevelDO memberLevelDO = memberLevelMapper.selectById(memberQuery.getLevelId());
        if (memberLevelDO == null) {
            throw new BusinessException("会员等级不存在，请先添加会员等级信息");
        }
        return memberLevelDO;
    }
}
